package com.imy.lua.udmapping.node;

import android.content.Context;
import android.view.accessibility.AccessibilityEvent;

import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;

import com.immomo.mls.LuaViewManager;
import com.immomo.mls.MLSBuilder;
import com.immomo.mls.wrapper.ILuaValueGetter;
import com.imy.common.util.ALog;

import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaString;
import org.luaj.vm2.LuaUserdata;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.utils.LuaApiUsed;

/**
 * Created by MLN Template 注册方法： Register.newUDHolder(UDAccessibilityEvent.LUA_CLASS_NAME, UDAccessibilityEvent.class, true)
 */
@LuaApiUsed
public class UDAccessibilityEvent extends LuaUserdata<AccessibilityEvent> {
    /**
     * Lua类名
     */
    public static final String LUA_CLASS_NAME = "AccessibilityEvent";


    public static final String[] methods = {
        "getPackageName",
        "getClassName",
        "getText",
    };
    @LuaApiUsed
    public LuaValue[] getText(LuaValue[] luaValues){
        return varargsOf(LuaString.valueOf(javaUserdata.getText().toString()));
    }
    @LuaApiUsed
    public LuaValue[] getClassName(LuaValue[] luaValues){
        return varargsOf(LuaString.valueOf(javaUserdata.getClassName().toString()));
    }

    @LuaApiUsed
    public LuaValue[] getPackageName(LuaValue[] luaValues) {
        ALog.i("getClassName:");
        return varargsOf(LuaString.valueOf(javaUserdata.getPackageName().toString()));
    }

    //@LuaApiUsed
    //public LuaValue[] getClassName(LuaValue[] luaValues){
    //
    //    return varargsOf()
    //}
    //<editor-fold desc="Constructors">

    /**
     * 提供给Lua的构造函数 必须存在
     *
     * @param L 虚拟机底层地址
     * @param v 初始化参数，非空，但长度可能为0
     */
    @LuaApiUsed
    protected UDAccessibilityEvent(long L, @NonNull LuaValue[] v) {
        super(L, v);
        /// 必须完成包裹对象的初始化
    }

    /**
     * 提供给Java的构造函数
     *
     * @param g 虚拟机
     * @param o 初始化对象
     */
    @LuaApiUsed
    public UDAccessibilityEvent(@NonNull Globals g, AccessibilityEvent o) {
        super(g, o);
    }
    //</editor-fold>

    /**
     * 获取上下文，一般为Activity
     */
    protected Context getContext() {
        LuaViewManager m = (LuaViewManager) getGlobals().getJavaUserdata();
        return m != null ? m.context : null;
    }



    /**
     * 此对象被Lua GC时调用，可不实现
     * 可做相关释放操作
     */
    @CallSuper
    @Override
    protected void __onLuaGc() {
        super.__onLuaGc();
        javaUserdata=null;
    }

    /**
     * Lua判断相等时，可能会调用此方法
     * 可通过实现{@link #equals(Object)}来实现
     */
    //@Override
    //protected boolean __onLuaEq(Object other) { }

    /**
     * 若{@link #__onLuaEq}默认实现，则Lua判断相等时，可能调用此方法
     */
    //@Override
    //public boolean equals(Object o) { }
    //</editor-fold>

    //<editor-fold desc="Auto Convert">
    /**
     * 将Lua类型自动转换为Java类型，一般在基础类中使用，或选择默认转换方式
     * 注册方法：
     * @see com.immomo.mls.MLSBuilder#registerCovert(MLSBuilder.CHolder...)
     * @see MLSBuilder.CHolder
     */
    /*public static final IJavaObjectGetter<UDAccessibilityEvent, AccessibilityEvent> J = new IJavaObjectGetter<UDAccessibilityEvent, AccessibilityEvent>() {
        @Override
        public AccessibilityEvent getJavaObject(UDAccessibilityEvent lv) {
            return lv.getJavaUserdata();
        }
    };*/

    /**
     * 将Java类型转换为Lua类型，一般在基础类中使用，或选择默认转换方式
     * 注册方法：
     * @see com.immomo.mls.MLSBuilder#registerCovert(MLSBuilder.CHolder...)
     * @see MLSBuilder.CHolder
     */
    public static final ILuaValueGetter<UDAccessibilityEvent, AccessibilityEvent> L = new ILuaValueGetter<UDAccessibilityEvent, AccessibilityEvent>() {
        @Override
        public UDAccessibilityEvent newInstance(Globals g, AccessibilityEvent obj) {
            return new UDAccessibilityEvent(g, obj);
        }
    };
    //</editor-fold>
}
